﻿'    This file is part of The Global Distribution System.

'    The Global Distribution System is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.

'    The Global Distribution System is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with The Global Distribution System.  If not, see <http://www.gnu.org/licenses/>.

Imports System.Windows.Forms

Public Class Form_ClientManager
    Public WithEvents ClientHandler As ClientHandler

    Public Sub New(ByRef CH As ClientHandler)
        InitializeComponent()
        ClientHandler = CH
    End Sub

    Private Sub Form_ClientManager_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.LoadUsers()
    End Sub

    Private Delegate Sub dClientChatJoin(ByVal ip As String, ByVal name As String)

    Private Sub ClientChatJoin(ByVal ip As String, ByVal name As String)
        If LV_Users.InvokeRequired Then
            LV_Users.Invoke(New dClientChatJoin(AddressOf ClientChatJoin), ip, name)
        Else
            For i As Integer = 0 To LV_Users.Items.Count - 1
                If LV_Users.Items(i).Text.Equals(ip) Then
                    LV_Users.Items(i).SubItems.Add(New ListViewItem.ListViewSubItem With {.Text = name})
                    Return
                End If
            Next
        End If
    End Sub

    Private Sub ClientError(ByVal ip As String, ByVal name As String)
        If LV_Users.InvokeRequired Then
            LV_Users.Invoke(New dClientChatJoin(AddressOf ClientError), ip, Nothing)
        Else
            For i As Integer = 0 To LV_Users.Items.Count - 1
                If LV_Users.Items(i).Text.Equals(ip) Then LV_Users.Items.RemoveAt(i) : Return
            Next
        End If
    End Sub

    Private Sub ClientLeavesChat(ByVal ip As String, ByVal name As String)
        If LV_Users.InvokeRequired Then
            LV_Users.Invoke(New dClientChatJoin(AddressOf ClientLeavesChat), ip, name)
        Else
            For i As Integer = 0 To LV_Users.Items.Count - 1
                If LV_Users.Items(i).Text.Equals(ip) Then LV_Users.Items(i).SubItems.RemoveAt(1) : Return
            Next
        End If
    End Sub

    Private Sub ClientLogin(ByVal ip As String, ByVal name As String)
        If LV_Users.InvokeRequired() Then
            LV_Users.Invoke(New dClientChatJoin(AddressOf ClientLogin), ip, name)
        Else
            LV_Users.Items.Add(New ListViewItem With {.Text = ip})
        End If
    End Sub

    Private Sub ClientHandler_ClientChatJoin(ByVal IP As String, ByVal name As String) Handles ClientHandler.ClientChatJoin
        ClientChatJoin(IP, name)
    End Sub

    Private Sub ClientHandler_ClientError(ByVal IP As String, ByVal ErrorData As System.Exception) Handles ClientHandler.ClientError
        ClientError(IP, String.Empty)
    End Sub

    Private Sub ClientHandler_ClientLeavesChat(ByVal IP As String) Handles ClientHandler.ClientLeavesChat
        ClientLeavesChat(IP, String.Empty)
    End Sub

    Private Sub ClientHandler_ClientLogin(ByVal IP As String) Handles ClientHandler.ClientLogin
        ClientLogin(IP, String.Empty)
    End Sub

    ''' <summary>
    ''' Loads the Users into the listview.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadUsers()
        LV_Users.Items.Clear()
        For Each item As ClientHandler.StoredClient In ClientHandler.ReturnOrganizedList
            Dim LVI As New ListViewItem
            LVI.Text = item.IP
            If Not item.ChatName.Equals(String.Empty) Then
                LVI.SubItems.Add(New ListViewItem.ListViewSubItem With {.Text = item.ChatName})
            End If
            LV_Users.Items.Add(LVI)
        Next
    End Sub

End Class